<?php
/**
 * 系统日志管理.
 * User: Administrator
 * Date: 2018/10/30 0030
 * Time: 09:45
 */
namespace app\admin\controller;

use service\DataService;
use think\Db;
class Log extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemLog';

    /**
     * 日志列表
     * @return array|string
     */
    public function index()
    {
        if($this->request->isPost()){
            $param = $this->request->post();
            $db = Db::name($this->table);
            foreach (['action', 'content', 'username'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike($key, "%{$param[$key]}%");
            }
            if (isset($param['date']) && $param['date'] !== '') {
                list($start, $end) = explode('-', str_replace(' ', '', $param['date']));
                $db->whereBetween('create_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
            }
            $db->order('id desc');
            return parent::_list($db,$param);
        }
        // 日志行为类别
        $actions = Db::name($this->table)->group('action')->column('action');
        $this->title = '网站配置 > 操作日志';
        return $this->fetch('', ['title' => $this->title,'actions'=>$actions]);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        $ip = new \Ip2Region();
        foreach ($data as &$vo) {
            $result = $ip->btreeSearch($vo['ip']);
            $vo['isp'] = isset($result['region']) ? $result['region'] : '';
            $vo['isp'] = str_replace(['|0|0|0|0', '0', '|'], ['', '', ' '], $vo['isp']);
        }
    }

    /**
     * 日志删除操作
     */
    public function del()
    {
        $status = DataService::update($this->table);
        $rst = doReturn('日志删除成功!','日志删除失败, 请稍候再试!',$status);
        $this->resultArr($rst);
    }

}
